package cn.iocoder.yudao.module.brokerage.merchant.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreQueryWrapper;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.business.basic.service.impl.BaseService;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.api.member.user.MemberUserApi;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.api.shop.info.dto.ShopInfoDTO;
import cn.iocoder.yudao.module.brokerage.merchant.dao.BrokerageMerchantRecordMapper;
import cn.iocoder.yudao.module.brokerage.merchant.pojo.po.AgencyBrokerageRecordDO;
import cn.iocoder.yudao.module.brokerage.merchant.pojo.vo.AdminBrokerageMerchantRecordQueryDTO;
import cn.iocoder.yudao.module.brokerage.merchant.pojo.vo.AdminBrokerageMerchantRecordVO;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

/**
 * 分佣记录 Service 实现类
 *
 * @author 山野羡民
 */
@Slf4j
@Service
@Validated
public class AdminBrokerageMerchantRecordServiceImpl extends BaseService<AgencyBrokerageRecordDO, AdminBrokerageMerchantRecordVO, AdminBrokerageMerchantRecordQueryDTO> implements AdminBrokerageMerchantRecordService {
    @Resource
    private BrokerageMerchantRecordMapper brokerageMerchantRecordMapper;

    @Resource
    private MemberUserApi memberUserApi;
    @Resource
    private ShopInfoApi shopInfoApi;

    @Override
    protected BrokerageMerchantRecordMapper getInjectedMapper() {
        return brokerageMerchantRecordMapper;
    }

    @Nonnull
    @Override
    protected CoreQueryWrapper<AgencyBrokerageRecordDO> buildQueryWrapper(AdminBrokerageMerchantRecordQueryDTO param) {
        CoreQueryWrapper<AgencyBrokerageRecordDO> wrapper = super.buildQueryWrapper(param);
        wrapper.eqIfPresent("status", param.getStatus());
        wrapper.eq(StrUtil.isNotBlank(param.getTradeNo()), "trade_no", param.getTradeNo());
        wrapper.orderByDesc("update_time");
        wrapper.orderByDesc("id");
        return wrapper;
    }

    @Override
    public PageResult<AdminBrokerageMerchantRecordVO> getPage(Integer current, Integer size, @Nonnull AdminBrokerageMerchantRecordQueryDTO param) {
        PageResult<AdminBrokerageMerchantRecordVO> page = super.getPage(current, size, param);
        if (CollUtil.isEmpty(page.getList())) {
            return PageResult.empty();
        }
        Set<Long> agencyUserIds = convertSet(page.getList(), AdminBrokerageMerchantRecordVO::getUserId);
        Set<Long> userIds = new HashSet<>(agencyUserIds);
        Set<Long> sourceUserIds = convertSet(page.getList(), AdminBrokerageMerchantRecordVO::getSourceUserId);
        userIds.addAll(sourceUserIds);
        Map<Long, UserSimpleVO> userMap = memberUserApi.getUserSimpleMap(userIds);
        Set<Long> shopIds = convertSet(page.getList(), AdminBrokerageMerchantRecordVO::getSourceShopId);
        Map<Long, ShopInfoDTO> shopMap = shopInfoApi.getShopInfoMap(shopIds);
        page.getList().forEach(item -> {
            item.setUser(userMap.getOrDefault(item.getUserId(), UserSimpleVO.ANONYMOUS));
            item.setSourceUser(userMap.getOrDefault(item.getSourceUserId(), UserSimpleVO.ANONYMOUS));
            item.setSourceShop(shopMap.getOrDefault(item.getSourceShopId(), ShopInfoDTO.FALLBACK));
        });
        return page;
    }

}